package com.my.ddd_sample_collections.aggregate_change_tracking.abstract_domain;

import com.my.ddd_sample_collections.aggregate_change_tracking.repository_framework.DomainObject;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.function.Predicate;

public class MyArrayList<T extends DomainObject> extends ArrayList {
    
    private final DomainObject parent;
    private final String propertyName;
    
    /**
     * TODO 实现自定义的List，以跟踪删除集合条目的情况
     */
    public MyArrayList(DomainObject parent, String propertyName) {
        this.parent = parent;
        this.propertyName = propertyName;
    }
    
    @Override
    protected void removeRange(int fromIndex, int toIndex) {
        throw new UnsupportedOperationException("不支持removeRange");
    }
    
    @Override
    public boolean remove(Object o) {
        return remove((T) o);
    }
    
    public boolean remove(T o) {
        //TODO track change of delete
        parent.trackRemoveItem(parent, propertyName, o);
        return super.remove(o);
    }
    
    @Override
    public Object remove(int index) {
        //TODO track change of delete
        return remove(get(index));
    }
    
    @Override
    public boolean removeAll(Collection c) {
        //TODO track change of delete
        return super.removeAll(c);
    }
    
    @Override
    public boolean removeIf(Predicate filter) {
        throw new UnsupportedOperationException("");
    }
    
    @Override
    public Object removeFirst() {
        //TODO track change of delete
        return super.removeFirst();
    }
    
    @Override
    public Object removeLast() {
        //TODO track change of delete
        return super.removeLast();
    }
    
    @Override
    public Iterator iterator() {
        throw new UnsupportedOperationException();
    }
}
